/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ListUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.datasource.DataSourceManager;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxLock;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Formatter;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DbDataSource
/*     */ {
/*     */   public static final String TABLE_DATA_SOURCE_ITEM = "data_source_item";
/*     */   
/*     */   public static class DataSourceItem
/*     */   {
/*     */     public int folderId;
/*     */     public int itemId;
/*     */     public String remoteId;
/*     */     public Metadata md;
/*  45 */     public int itemFlags = -1;
/*     */     
/*     */     public DataSourceItem(int f, int i, String r, Metadata m) {
/*  48 */       this.folderId = f;
/*  49 */       this.itemId = i;
/*  50 */       this.remoteId = r;
/*  51 */       this.md = m;
/*     */     }
/*     */     
/*     */     public DataSourceItem(int f, int i, String r, Metadata m, int fl) {
/*  55 */       this(f, i, r, m);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public static void addMapping(DataSource ds, DataSourceItem item)
/*     */     throws ServiceException
/*     */   {
/*  63 */     addMapping(ds, item, false);
/*     */   }
/*     */   
/*     */   public static void addMapping(DataSource ds, DataSourceItem item, boolean isBatch) throws ServiceException {
/*  67 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/*  69 */     DbPool.DbConnection conn = null;
/*  70 */     PreparedStatement stmt = null;
/*  71 */     String dataSourceId = ds.getId();
/*     */     
/*  73 */     if (item.remoteId == null) {
/*  74 */       item.remoteId = "";
/*     */     }
/*  76 */     ZimbraLog.datasource.debug("Adding mapping for dataSource %s: itemId(%d), remoteId(%s)", new Object[] { ds.getName(), Integer.valueOf(item.itemId), item.remoteId });
/*     */     try
/*     */     {
/*  79 */       if (isBatch) {
/*  80 */         conn = mbox.getOperationConnection();
/*     */       } else {
/*  82 */         conn = DbPool.getConnection(mbox);
/*     */       }
/*  84 */       StringBuilder sb = new StringBuilder();
/*  85 */       sb.append("INSERT INTO ");
/*  86 */       sb.append(getTableName(mbox));
/*  87 */       sb.append(" (");
/*  88 */       sb.append(DbMailItem.MAILBOX_ID);
/*  89 */       sb.append("data_source_id, item_id, folder_id, remote_id, metadata) VALUES (");
/*  90 */       sb.append(DbMailItem.MAILBOX_ID_VALUE);
/*  91 */       sb.append("?, ?, ?, ?, ?)");
/*  92 */       if (Db.supports(Db.Capability.ON_DUPLICATE_KEY)) {
/*  93 */         sb.append(" ON DUPLICATE KEY UPDATE data_source_id = ?, item_id = ?, folder_id = ?, remote_id = ?, metadata = ?");
/*     */       }
/*  95 */       stmt = conn.prepareStatement(sb.toString());
/*  96 */       int i = 1;
/*  97 */       i = DbMailItem.setMailboxId(stmt, mbox, i);
/*  98 */       stmt.setString(i++, dataSourceId);
/*  99 */       stmt.setInt(i++, item.itemId);
/* 100 */       stmt.setInt(i++, item.folderId);
/* 101 */       stmt.setString(i++, item.remoteId);
/* 102 */       stmt.setString(i++, DbMailItem.checkMetadataLength(item.md == null ? null : item.md.toString()));
/* 103 */       if (Db.supports(Db.Capability.ON_DUPLICATE_KEY)) {
/* 104 */         stmt.setString(i++, dataSourceId);
/* 105 */         stmt.setInt(i++, item.itemId);
/* 106 */         stmt.setInt(i++, item.folderId);
/* 107 */         stmt.setString(i++, item.remoteId);
/* 108 */         stmt.setString(i++, DbMailItem.checkMetadataLength(item.md == null ? null : item.md.toString()));
/*     */       }
/* 110 */       stmt.executeUpdate();
/* 111 */       if (!isBatch) {
/* 112 */         conn.commit();
/*     */       }
/*     */     } catch (SQLException e) {
/* 115 */       if ((!Db.supports(Db.Capability.ON_DUPLICATE_KEY)) && (Db.errorMatches(e, Db.Error.DUPLICATE_ROW))) {
/* 116 */         DbPool.closeStatement(stmt);
/* 117 */         if (!isBatch) {
/* 118 */           DbPool.quietClose(conn);
/*     */         }
/* 120 */         updateMapping(ds, item, isBatch);
/*     */       } else {
/* 122 */         throw ServiceException.FAILURE("Unable to add mapping for dataSource " + ds.getName(), e);
/*     */       }
/*     */     } finally {
/* 125 */       DbPool.closeStatement(stmt);
/* 126 */       if (!isBatch) {
/* 127 */         DbPool.quietClose(conn);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void updateMapping(DataSource ds, DataSourceItem item) throws ServiceException {
/* 133 */     updateMapping(ds, item, false);
/*     */   }
/*     */   
/*     */   public static void updateMapping(DataSource ds, DataSourceItem item, boolean isBatch) throws ServiceException {
/* 137 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 139 */     ZimbraLog.datasource.debug("Updating mapping for dataSource %s: itemId(%d), remoteId(%s)", new Object[] { ds.getName(), Integer.valueOf(item.itemId), item.remoteId });
/*     */     
/* 141 */     DbPool.DbConnection conn = null;
/* 142 */     PreparedStatement stmt = null;
/*     */     try {
/* 144 */       if (isBatch) {
/* 145 */         conn = mbox.getOperationConnection();
/*     */       } else {
/* 147 */         conn = DbPool.getConnection(mbox);
/*     */       }
/* 149 */       if ((!Db.supports(Db.Capability.ON_DUPLICATE_KEY)) && (!hasMapping(ds, item.itemId)))
/*     */       {
/*     */ 
/* 152 */         StringBuilder sb = new StringBuilder();
/* 153 */         sb.append("UPDATE ");
/* 154 */         sb.append(getTableName(mbox));
/* 155 */         sb.append(" SET folder_id = ?, item_id = ?, metadata = ? WHERE ");
/* 156 */         sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
/* 157 */         sb.append(" remote_id = ?");
/* 158 */         stmt = conn.prepareStatement(sb.toString());
/* 159 */         int i = 1;
/* 160 */         stmt.setInt(i++, item.folderId);
/* 161 */         stmt.setInt(i++, item.itemId);
/* 162 */         stmt.setString(i++, DbMailItem.checkMetadataLength(item.md == null ? null : item.md.toString()));
/* 163 */         i = DbMailItem.setMailboxId(stmt, mbox, i);
/* 164 */         stmt.setString(i++, item.remoteId);
/*     */       } else {
/* 166 */         StringBuilder sb = new StringBuilder();
/* 167 */         sb.append("UPDATE ");
/* 168 */         sb.append(getTableName(mbox));
/* 169 */         sb.append(" SET folder_id = ?, remote_id = ?, metadata = ? WHERE ");
/* 170 */         sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
/* 171 */         sb.append(" item_id = ?");
/* 172 */         stmt = conn.prepareStatement(sb.toString());
/* 173 */         int i = 1;
/* 174 */         stmt.setInt(i++, item.folderId);
/* 175 */         stmt.setString(i++, item.remoteId);
/* 176 */         stmt.setString(i++, DbMailItem.checkMetadataLength(item.md == null ? null : item.md.toString()));
/* 177 */         i = DbMailItem.setMailboxId(stmt, mbox, i);
/* 178 */         stmt.setInt(i++, item.itemId);
/*     */       }
/* 180 */       stmt.executeUpdate();
/* 181 */       if (!isBatch) {
/* 182 */         conn.commit();
/*     */       }
/*     */     } catch (SQLException e) {
/* 185 */       throw ServiceException.FAILURE("Unable to update mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 187 */       DbPool.closeStatement(stmt);
/* 188 */       if (!isBatch) {
/* 189 */         DbPool.quietClose(conn);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteMappings(DataSource ds, Collection<Integer> itemIds) throws ServiceException {
/* 195 */     deleteMappings(ds, itemIds, false);
/*     */   }
/*     */   
/*     */   public static void deleteMappings(DataSource ds, Collection<Integer> itemIds, boolean isBatch) throws ServiceException {
/* 199 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 201 */     ZimbraLog.datasource.debug("Deleting %d mappings for dataSource %s", new Object[] { Integer.valueOf(itemIds.size()), ds.getName() });
/* 202 */     List<List<Integer>> splitIds = ListUtil.split(itemIds, Db.getINClauseBatchSize());
/* 203 */     DbPool.DbConnection conn = null;
/* 204 */     PreparedStatement stmt = null;
/*     */     try {
/* 206 */       if (isBatch) {
/* 207 */         conn = mbox.getOperationConnection();
/*     */       } else {
/* 209 */         conn = DbPool.getConnection(mbox);
/*     */       }
/* 211 */       int numRows = 0;
/* 212 */       for (List<Integer> curIds : splitIds) {
/* 213 */         StringBuilder sb = new StringBuilder();
/* 214 */         sb.append("DELETE FROM ");
/* 215 */         sb.append(getTableName(mbox));
/* 216 */         sb.append(" WHERE ");
/* 217 */         sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
/* 218 */         sb.append(" data_source_id = ? AND ");
/* 219 */         sb.append(DbUtil.whereIn("item_id", curIds.size()));
/* 220 */         stmt = conn.prepareStatement(sb.toString());
/*     */         
/* 222 */         int i = 1;
/* 223 */         i = DbMailItem.setMailboxId(stmt, mbox, i);
/* 224 */         stmt.setString(i++, ds.getId());
/* 225 */         for (Iterator i$ = curIds.iterator(); i$.hasNext();) { int itemId = ((Integer)i$.next()).intValue();
/* 226 */           stmt.setInt(i++, itemId);
/*     */         }
/* 228 */         numRows += stmt.executeUpdate();
/* 229 */         if (!isBatch) {
/* 230 */           conn.commit();
/*     */         }
/* 232 */         stmt.close();
/*     */       }
/* 234 */       ZimbraLog.datasource.debug("Deleted %d mappings for %s", new Object[] { Integer.valueOf(numRows), ds.getName() });
/*     */     } catch (SQLException e) {
/* 236 */       throw ServiceException.FAILURE("Unable to delete mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 238 */       DbPool.closeStatement(stmt);
/* 239 */       if (!isBatch) {
/* 240 */         DbPool.quietClose(conn);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteAllMappings(DataSource ds) throws ServiceException {
/* 246 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 248 */     ZimbraLog.datasource.debug("Deleting all mappings for dataSource %s", new Object[] { ds.getName() });
/* 249 */     DbPool.DbConnection conn = null;
/* 250 */     PreparedStatement stmt = null;
/*     */     try {
/* 252 */       conn = DbPool.getConnection(mbox);
/* 253 */       StringBuilder sb = new StringBuilder();
/* 254 */       sb.append("DELETE FROM ");
/* 255 */       sb.append(getTableName(mbox));
/* 256 */       sb.append(" WHERE ");
/* 257 */       sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
/* 258 */       sb.append(" data_source_id = ?");
/* 259 */       stmt = conn.prepareStatement(sb.toString());
/* 260 */       int i = 1;
/* 261 */       i = DbMailItem.setMailboxId(stmt, mbox, i);
/* 262 */       stmt.setString(i++, ds.getId());
/* 263 */       int numRows = stmt.executeUpdate();
/* 264 */       conn.commit();
/* 265 */       ZimbraLog.datasource.debug("Deleted %d mappings for %s", new Object[] { Integer.valueOf(numRows), ds.getName() });
/*     */     } catch (SQLException e) {
/* 267 */       throw ServiceException.FAILURE("Unable to delete mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 269 */       DbPool.closeStatement(stmt);
/* 270 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteMapping(DataSource ds, int itemId) throws ServiceException {
/* 275 */     deleteMapping(ds, itemId, false);
/*     */   }
/*     */   
/*     */   public static void deleteMapping(DataSource ds, int itemId, boolean isBatch) throws ServiceException {
/* 279 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 281 */     ZimbraLog.datasource.debug("Deleting mappings for dataSource %s itemId %d", new Object[] { ds.getName(), Integer.valueOf(itemId) });
/*     */     
/* 283 */     DbPool.DbConnection conn = null;
/* 284 */     PreparedStatement stmt = null;
/*     */     try {
/* 286 */       if (isBatch) {
/* 287 */         conn = mbox.getOperationConnection();
/*     */       } else {
/* 289 */         conn = DbPool.getConnection(mbox);
/*     */       }
/* 291 */       StringBuilder sb = new StringBuilder();
/* 292 */       sb.append("DELETE FROM ");
/* 293 */       sb.append(getTableName(mbox));
/* 294 */       sb.append(" WHERE ");
/* 295 */       sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
/* 296 */       sb.append(" data_source_id = ? AND");
/* 297 */       sb.append(" item_id = ?");
/* 298 */       stmt = conn.prepareStatement(sb.toString());
/* 299 */       int i = 1;
/* 300 */       i = DbMailItem.setMailboxId(stmt, mbox, i);
/* 301 */       stmt.setString(i++, ds.getId());
/* 302 */       stmt.setInt(i++, itemId);
/* 303 */       int numRows = stmt.executeUpdate();
/* 304 */       if (!isBatch) {
/* 305 */         conn.commit();
/*     */       }
/* 307 */       ZimbraLog.datasource.debug("Deleted %d mappings for %s", new Object[] { Integer.valueOf(numRows), ds.getName() });
/*     */     } catch (SQLException e) {
/* 309 */       throw ServiceException.FAILURE("Unable to delete mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 311 */       DbPool.closeStatement(stmt);
/* 312 */       if (!isBatch) {
/* 313 */         DbPool.quietClose(conn);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static Collection<DataSourceItem> deleteAllMappingsInFolder(DataSource ds, int folderId) throws ServiceException {
/* 319 */     return deleteAllMappingsInFolder(ds, folderId, false);
/*     */   }
/*     */   
/*     */   public static Collection<DataSourceItem> deleteAllMappingsInFolder(DataSource ds, int folderId, boolean isBatch) throws ServiceException {
/* 323 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 325 */     ArrayList<DataSourceItem> items = new ArrayList();
/*     */     
/* 327 */     ZimbraLog.datasource.debug("Deleting all mappings for dataSource %s in folder %d", new Object[] { ds.getName(), Integer.valueOf(folderId) });
/*     */     
/* 329 */     mbox.lock.lock();
/*     */     try {
/* 331 */       DbPool.DbConnection conn = null;
/* 332 */       PreparedStatement stmt = null;
/*     */       try {
/* 334 */         if (isBatch) {
/* 335 */           conn = mbox.getOperationConnection();
/*     */         } else {
/* 337 */           conn = DbPool.getConnection(mbox);
/*     */         }
/* 339 */         String dataSourceTable = getTableName(mbox);
/* 340 */         String IN_THIS_MAILBOX_AND = dataSourceTable + ".mailbox_id = ? AND ";
/* 341 */         StringBuilder sb = new StringBuilder();
/* 342 */         sb.append("DELETE FROM ");
/* 343 */         sb.append(dataSourceTable);
/* 344 */         sb.append(" WHERE ");
/* 345 */         sb.append(IN_THIS_MAILBOX_AND);
/* 346 */         sb.append("  data_source_id = ? AND folder_id = ?");
/* 347 */         stmt = conn.prepareStatement(sb.toString());
/* 348 */         int pos = 1;
/* 349 */         pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 350 */         stmt.setString(pos++, ds.getId());
/* 351 */         stmt.setInt(pos++, folderId);
/* 352 */         int numRows = stmt.executeUpdate();
/* 353 */         if (!isBatch) {
/* 354 */           conn.commit();
/*     */         }
/* 356 */         stmt.close();
/* 357 */         ZimbraLog.datasource.debug("Deleted %d mappings for %s", new Object[] { Integer.valueOf(numRows), ds.getName() });
/*     */       } catch (SQLException e) {
/* 359 */         throw ServiceException.FAILURE("Unable to delete mapping for dataSource " + ds.getName(), e);
/*     */       } finally {
/* 361 */         DbPool.closeStatement(stmt);
/* 362 */         if (!isBatch) {
/* 363 */           DbPool.quietClose(conn);
/*     */         }
/*     */       }
/*     */     } finally {
/* 367 */       mbox.lock.release();
/*     */     }
/* 369 */     return items;
/*     */   }
/*     */   
/*     */   public static boolean hasMapping(DataSource ds, int itemId) throws ServiceException {
/* 373 */     DataSourceItem item = getMapping(ds, itemId);
/* 374 */     return item.remoteId != null;
/*     */   }
/*     */   
/*     */   public static Collection<DataSourceItem> getAllMappings(DataSource ds) throws ServiceException {
/* 378 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 380 */     ArrayList<DataSourceItem> items = new ArrayList();
/*     */     
/* 382 */     ZimbraLog.datasource.debug("Get all mappings for %s", new Object[] { ds.getName() });
/*     */     
/* 384 */     DbPool.DbConnection conn = null;
/* 385 */     PreparedStatement stmt = null;
/* 386 */     ResultSet rs = null;
/*     */     try {
/* 388 */       conn = DbPool.getConnection(mbox);
/* 389 */       StringBuilder sb = new StringBuilder();
/* 390 */       sb.append("SELECT item_id, folder_id, remote_id, metadata FROM ");
/* 391 */       sb.append(getTableName(mbox));
/* 392 */       sb.append(" WHERE ");
/* 393 */       sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
/* 394 */       sb.append("  data_source_id = ?");
/* 395 */       stmt = conn.prepareStatement(sb.toString());
/* 396 */       int i = 1;
/* 397 */       i = DbMailItem.setMailboxId(stmt, mbox, i);
/* 398 */       stmt.setString(i++, ds.getId());
/* 399 */       rs = stmt.executeQuery();
/* 400 */       while (rs.next()) {
/* 401 */         Metadata md = null;
/* 402 */         String buf = DbMailItem.decodeMetadata(rs.getString(4));
/* 403 */         if (buf != null)
/* 404 */           md = new Metadata(buf);
/* 405 */         items.add(new DataSourceItem(rs.getInt(2), rs.getInt(1), rs.getString(3), md));
/*     */       }
/* 407 */       rs.close();
/* 408 */       stmt.close();
/*     */     } catch (SQLException e) {
/* 410 */       throw ServiceException.FAILURE("Unable to get mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 412 */       DbPool.closeResults(rs);
/* 413 */       DbPool.closeStatement(stmt);
/* 414 */       DbPool.quietClose(conn);
/*     */     }
/* 416 */     return items;
/*     */   }
/*     */   
/*     */   public static Collection<DataSourceItem> getAllMappingsInFolder(DataSource ds, int folderId) throws ServiceException {
/* 420 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 422 */     ArrayList<DataSourceItem> items = new ArrayList();
/*     */     
/* 424 */     ZimbraLog.datasource.debug("Get all mappings for %s in folder %d", new Object[] { ds.getName(), Integer.valueOf(folderId) });
/*     */     
/* 426 */     DbPool.DbConnection conn = null;
/* 427 */     PreparedStatement stmt = null;
/* 428 */     ResultSet rs = null;
/*     */     try {
/* 430 */       conn = DbPool.getConnection(mbox);
/*     */       
/* 432 */       String thisTable = getTableName(mbox);
/* 433 */       String IN_THIS_MAILBOX_AND = thisTable + ".mailbox_id = ? AND ";
/* 434 */       StringBuilder sb = new StringBuilder();
/* 435 */       sb.append("SELECT item_id, remote_id, ").append(thisTable).append(".metadata FROM ");
/* 436 */       sb.append(thisTable);
/* 437 */       sb.append(" WHERE ");
/* 438 */       sb.append(IN_THIS_MAILBOX_AND);
/* 439 */       sb.append("  data_source_id = ? AND folder_id = ?");
/* 440 */       stmt = conn.prepareStatement(sb.toString());
/* 441 */       int pos = 1;
/* 442 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 443 */       stmt.setString(pos++, ds.getId());
/* 444 */       stmt.setInt(pos++, folderId);
/* 445 */       rs = stmt.executeQuery();
/* 446 */       while (rs.next()) {
/* 447 */         Metadata md = null;
/* 448 */         String buf = DbMailItem.decodeMetadata(rs.getString(3));
/* 449 */         if (buf != null)
/* 450 */           md = new Metadata(buf);
/* 451 */         items.add(new DataSourceItem(folderId, rs.getInt(1), rs.getString(2), md));
/*     */       }
/* 453 */       rs.close();
/* 454 */       stmt.close();
/*     */     } catch (SQLException e) {
/* 456 */       throw ServiceException.FAILURE("Unable to get mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 458 */       DbPool.closeResults(rs);
/* 459 */       DbPool.closeStatement(stmt);
/* 460 */       DbPool.quietClose(conn);
/*     */     }
/* 462 */     return items;
/*     */   }
/*     */   
/*     */   public static Collection<DataSourceItem> getAllMappingsAndFlagsInFolder(DataSource ds, int folderId) throws ServiceException {
/* 466 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 468 */     ArrayList<DataSourceItem> items = new ArrayList();
/*     */     
/* 470 */     ZimbraLog.datasource.debug("Get all mappings for %s in folder %d", new Object[] { ds.getName(), Integer.valueOf(folderId) });
/*     */     
/* 472 */     DbPool.DbConnection conn = null;
/* 473 */     PreparedStatement stmt = null;
/* 474 */     ResultSet rs = null;
/*     */     try {
/* 476 */       String thisTable = getTableName(mbox);
/* 477 */       String IN_THIS_MAILBOX_AND = thisTable + ".mailbox_id = ? AND ";
/* 478 */       String MBOX_JOIN = thisTable + ".mailbox_id = mi.mailbox_id AND ";
/* 479 */       conn = DbPool.getConnection(mbox);
/* 480 */       StringBuilder sb = new StringBuilder();
/* 481 */       sb.append("SELECT item_id, remote_id, ").append(thisTable).append(".metadata, mi.unread, mi.flags FROM ");
/* 482 */       sb.append(thisTable);
/* 483 */       sb.append("  LEFT OUTER JOIN " + DbMailItem.getMailItemTableName(mbox)).append(" mi ");
/* 484 */       sb.append("  ON ").append(MBOX_JOIN).append(thisTable).append(".item_id = mi.id ");
/* 485 */       sb.append(" WHERE ");
/* 486 */       sb.append(IN_THIS_MAILBOX_AND);
/* 487 */       sb.append("  data_source_id = ? AND ").append(thisTable).append(".folder_id = ?");
/* 488 */       stmt = conn.prepareStatement(sb.toString());
/* 489 */       int pos = 1;
/* 490 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 491 */       stmt.setString(pos++, ds.getId());
/* 492 */       stmt.setInt(pos++, folderId);
/* 493 */       rs = stmt.executeQuery();
/* 494 */       while (rs.next()) {
/* 495 */         Metadata md = null;
/* 496 */         String buf = DbMailItem.decodeMetadata(rs.getString(3));
/* 497 */         int unread = rs.getInt(4);
/* 498 */         int flags = rs.getInt(5);
/*     */         
/* 500 */         if (buf != null)
/* 501 */           md = new Metadata(buf);
/* 502 */         flags = unread > 0 ? flags | Flag.BITMASK_UNREAD : flags & (Flag.BITMASK_UNREAD ^ 0xFFFFFFFF);
/* 503 */         items.add(new DataSourceItem(folderId, rs.getInt(1), rs.getString(2), md, flags));
/*     */       }
/* 505 */       rs.close();
/* 506 */       stmt.close();
/*     */     } catch (SQLException e) {
/* 508 */       throw ServiceException.FAILURE("Unable to get mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 510 */       DbPool.closeResults(rs);
/* 511 */       DbPool.closeStatement(stmt);
/* 512 */       DbPool.quietClose(conn);
/*     */     }
/* 514 */     return items;
/*     */   }
/*     */   
/*     */   public static Collection<DataSourceItem> getAllMappingsForRemoteIdPrefix(DataSource ds, int folderId, String prefix) throws ServiceException
/*     */   {
/* 519 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 521 */     List<DataSourceItem> items = new ArrayList();
/*     */     
/* 523 */     DbPool.DbConnection conn = null;
/* 524 */     PreparedStatement stmt = null;
/* 525 */     ResultSet rs = null;
/*     */     try {
/* 527 */       conn = DbPool.getConnection(mbox);
/* 528 */       String db = DbMailbox.getDatabaseName(mbox);
/* 529 */       String dst = db + ".data_source_item";
/* 530 */       String mit = db + ".mail_item";
/* 531 */       Formatter fmt = new Formatter();
/* 532 */       fmt.format("SELECT item_id, remote_id, %s.metadata FROM %s", new Object[] { dst, dst });
/* 533 */       fmt.format(" INNER JOIN %s ON %s.item_id = %s.id", new Object[] { mit, dst, mit });
/* 534 */       fmt.format(" WHERE %s.mailbox_id = ?", new Object[] { dst });
/* 535 */       fmt.format(" AND data_source_id = ? AND folder_id = ?", new Object[0]);
/* 536 */       if (prefix != null) {
/* 537 */         fmt.format(" AND remote_id LIKE '%s%%'", new Object[] { prefix });
/*     */       }
/* 539 */       stmt = conn.prepareStatement(fmt.toString());
/* 540 */       stmt.setInt(1, mbox.getId());
/* 541 */       stmt.setString(2, ds.getId());
/* 542 */       stmt.setInt(3, folderId);
/* 543 */       rs = stmt.executeQuery();
/* 544 */       while (rs.next()) {
/* 545 */         String s = DbMailItem.decodeMetadata(rs.getString(3));
/* 546 */         Metadata md = s != null ? new Metadata(s) : null;
/* 547 */         items.add(new DataSourceItem(folderId, rs.getInt(1), rs.getString(2), md));
/*     */       }
/*     */     } catch (SQLException e) {
/* 550 */       throw ServiceException.FAILURE("Unable to get mapping for data source " + ds.getName(), e);
/*     */     } finally {
/* 552 */       DbPool.closeResults(rs);
/* 553 */       DbPool.closeStatement(stmt);
/* 554 */       DbPool.quietClose(conn);
/*     */     }
/* 556 */     return items;
/*     */   }
/*     */   
/*     */   public static DataSourceItem getMapping(DataSource ds, int itemId) throws ServiceException {
/* 560 */     return getMapping(ds, itemId, false);
/*     */   }
/*     */   
/*     */   public static DataSourceItem getMapping(DataSource ds, int itemId, boolean isBatch) throws ServiceException {
/* 564 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 566 */     int folderId = 0;
/* 567 */     String remoteId = null;
/* 568 */     Metadata md = null;
/*     */     
/* 570 */     ZimbraLog.datasource.debug("Get mapping for %s, itemId=%d", new Object[] { ds.getName(), Integer.valueOf(itemId) });
/*     */     
/* 572 */     DbPool.DbConnection conn = null;
/* 573 */     PreparedStatement stmt = null;
/* 574 */     ResultSet rs = null;
/*     */     try {
/* 576 */       if (isBatch) {
/* 577 */         conn = mbox.getOperationConnection();
/*     */       } else {
/* 579 */         conn = DbPool.getConnection(mbox);
/*     */       }
/* 581 */       StringBuilder sb = new StringBuilder();
/* 582 */       sb.append("SELECT folder_id, remote_id, metadata FROM ");
/* 583 */       sb.append(getTableName(mbox));
/* 584 */       sb.append(" WHERE ");
/* 585 */       sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
/* 586 */       sb.append("  data_source_id = ? AND item_id = ?");
/* 587 */       stmt = conn.prepareStatement(sb.toString());
/* 588 */       int i = 1;
/* 589 */       i = DbMailItem.setMailboxId(stmt, mbox, i);
/* 590 */       stmt.setString(i++, ds.getId());
/* 591 */       stmt.setInt(i++, itemId);
/* 592 */       rs = stmt.executeQuery();
/* 593 */       if (rs.next()) {
/* 594 */         folderId = rs.getInt(1);
/* 595 */         remoteId = rs.getString(2);
/* 596 */         String buf = DbMailItem.decodeMetadata(rs.getString(3));
/* 597 */         if (buf != null)
/* 598 */           md = new Metadata(buf);
/*     */       }
/* 600 */       rs.close();
/* 601 */       stmt.close();
/*     */     } catch (SQLException e) {
/* 603 */       throw ServiceException.FAILURE("Unable to get mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 605 */       DbPool.closeResults(rs);
/* 606 */       DbPool.closeStatement(stmt);
/* 607 */       if (!isBatch) {
/* 608 */         DbPool.quietClose(conn);
/*     */       }
/*     */     }
/* 611 */     return new DataSourceItem(folderId, itemId, remoteId, md);
/*     */   }
/*     */   
/*     */   public static DataSourceItem getReverseMapping(DataSource ds, String remoteId) throws ServiceException {
/* 615 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/*     */     
/* 617 */     int folderId = 0;
/* 618 */     int itemId = 0;
/* 619 */     Metadata md = null;
/*     */     
/* 621 */     ZimbraLog.datasource.debug("Get reverse mapping for %s, remoteId=%s", new Object[] { ds.getName(), remoteId });
/*     */     
/* 623 */     DbPool.DbConnection conn = null;
/* 624 */     PreparedStatement stmt = null;
/* 625 */     ResultSet rs = null;
/*     */     try {
/* 627 */       conn = DbPool.getConnection(mbox);
/* 628 */       StringBuilder sb = new StringBuilder();
/* 629 */       sb.append("SELECT item_id, folder_id, metadata FROM ");
/* 630 */       sb.append(getTableName(mbox));
/* 631 */       sb.append(" WHERE ");
/* 632 */       sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
/* 633 */       sb.append("  data_source_id = ? AND remote_id = ?");
/* 634 */       stmt = conn.prepareStatement(sb.toString());
/* 635 */       int i = 1;
/* 636 */       i = DbMailItem.setMailboxId(stmt, mbox, i);
/* 637 */       stmt.setString(i++, ds.getId());
/* 638 */       stmt.setString(i++, remoteId);
/* 639 */       rs = stmt.executeQuery();
/* 640 */       if (rs.next()) {
/* 641 */         itemId = rs.getInt(1);
/* 642 */         folderId = rs.getInt(2);
/* 643 */         String buf = DbMailItem.decodeMetadata(rs.getString(3));
/* 644 */         if (buf != null)
/* 645 */           md = new Metadata(buf);
/*     */       }
/* 647 */       rs.close();
/* 648 */       stmt.close();
/*     */     } catch (SQLException e) {
/* 650 */       throw ServiceException.FAILURE("Unable to get reverse mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 652 */       DbPool.closeResults(rs);
/* 653 */       DbPool.closeStatement(stmt);
/* 654 */       DbPool.quietClose(conn);
/*     */     }
/* 656 */     return new DataSourceItem(folderId, itemId, remoteId, md);
/*     */   }
/*     */   
/*     */   public static Collection<DataSourceItem> getMappings(DataSource ds, Collection<Integer> ids) throws ServiceException
/*     */   {
/* 661 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/* 662 */     int folderId = 0;
/* 663 */     int itemId = 0;
/* 664 */     String remoteId = null;
/* 665 */     Metadata md = null;
/* 666 */     List<List<Integer>> splitIds = ListUtil.split(ids, Db.getINClauseBatchSize());
/* 667 */     ArrayList<DataSourceItem> items = new ArrayList();
/*     */     
/* 669 */     ZimbraLog.datasource.debug("Get mappings for %s", new Object[] { ds.getName() });
/*     */     
/* 671 */     DbPool.DbConnection conn = null;
/* 672 */     PreparedStatement stmt = null;
/* 673 */     ResultSet rs = null;
/*     */     try {
/* 675 */       conn = DbPool.getConnection(mbox);
/* 676 */       for (List<Integer> curIds : splitIds) {
/* 677 */         StringBuilder sb = new StringBuilder();
/* 678 */         sb.append("SELECT item_id, remote_id, folder_id, metadata FROM ");
/* 679 */         sb.append(getTableName(mbox));
/* 680 */         sb.append(" WHERE ");
/* 681 */         sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
/* 682 */         sb.append(" data_source_id = ? AND ");
/* 683 */         sb.append(DbUtil.whereIn("item_id", curIds.size()));
/* 684 */         stmt = conn.prepareStatement(sb.toString());
/* 685 */         int i = 1;
/* 686 */         i = DbMailItem.setMailboxId(stmt, mbox, i);
/* 687 */         stmt.setString(i++, ds.getId());
/* 688 */         for (Iterator i$ = curIds.iterator(); i$.hasNext();) { int uid = ((Integer)i$.next()).intValue();
/* 689 */           stmt.setInt(i++, uid); }
/* 690 */         rs = stmt.executeQuery();
/* 691 */         while (rs.next()) {
/* 692 */           itemId = rs.getInt(1);
/* 693 */           remoteId = rs.getString(2);
/* 694 */           folderId = rs.getInt(3);
/* 695 */           String buf = DbMailItem.decodeMetadata(rs.getString(4));
/* 696 */           if (buf != null)
/* 697 */             md = new Metadata(buf);
/* 698 */           items.add(new DataSourceItem(folderId, itemId, remoteId, md));
/*     */         }
/* 700 */         rs.close();
/* 701 */         stmt.close();
/*     */       }
/*     */     } catch (SQLException e) {
/* 704 */       throw ServiceException.FAILURE("Unable to get mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 706 */       DbPool.closeResults(rs);
/* 707 */       DbPool.closeStatement(stmt);
/* 708 */       DbPool.quietClose(conn);
/*     */     }
/* 710 */     return items;
/*     */   }
/*     */   
/*     */   public static Collection<DataSourceItem> getReverseMappings(DataSource ds, Collection<String> remoteIds) throws ServiceException
/*     */   {
/* 715 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(ds);
/* 716 */     int folderId = 0;
/* 717 */     int itemId = 0;
/*     */     
/* 719 */     Metadata md = null;
/* 720 */     List<List<String>> splitIds = ListUtil.split(remoteIds, Db.getINClauseBatchSize());
/* 721 */     ArrayList<DataSourceItem> items = new ArrayList();
/*     */     
/* 723 */     ZimbraLog.datasource.debug("Get reverse mappings for %s", new Object[] { ds.getName() });
/*     */     
/* 725 */     DbPool.DbConnection conn = null;
/* 726 */     PreparedStatement stmt = null;
/* 727 */     ResultSet rs = null;
/*     */     try {
/* 729 */       conn = DbPool.getConnection(mbox);
/* 730 */       for (List<String> curIds : splitIds) {
/* 731 */         StringBuilder sb = new StringBuilder();
/* 732 */         sb.append("SELECT item_id, remote_id, folder_id, metadata FROM ");
/* 733 */         sb.append(getTableName(mbox));
/* 734 */         sb.append(" WHERE ");
/* 735 */         sb.append(DbMailItem.IN_THIS_MAILBOX_AND);
/* 736 */         sb.append(" data_source_id = ? AND ");
/* 737 */         sb.append(DbUtil.whereIn("remote_id", curIds.size()));
/* 738 */         stmt = conn.prepareStatement(sb.toString());
/* 739 */         int i = 1;
/* 740 */         i = DbMailItem.setMailboxId(stmt, mbox, i);
/* 741 */         stmt.setString(i++, ds.getId());
/* 742 */         for (String uid : curIds)
/* 743 */           stmt.setString(i++, uid);
/* 744 */         rs = stmt.executeQuery();
/* 745 */         while (rs.next()) {
/* 746 */           itemId = rs.getInt(1);
/* 747 */           String remoteId = rs.getString(2);
/* 748 */           folderId = rs.getInt(3);
/* 749 */           String buf = DbMailItem.decodeMetadata(rs.getString(4));
/* 750 */           if (buf != null)
/* 751 */             md = new Metadata(buf);
/* 752 */           items.add(new DataSourceItem(folderId, itemId, remoteId, md));
/*     */         }
/* 754 */         rs.close();
/* 755 */         stmt.close();
/*     */       }
/*     */     } catch (SQLException e) {
/* 758 */       throw ServiceException.FAILURE("Unable to get reverse mapping for dataSource " + ds.getName(), e);
/*     */     } finally {
/* 760 */       DbPool.closeResults(rs);
/* 761 */       DbPool.closeStatement(stmt);
/* 762 */       DbPool.quietClose(conn);
/*     */     }
/* 764 */     return items;
/*     */   }
/*     */   
/*     */   public static String getTableName(int mailboxId, int groupId) {
/* 768 */     return DbMailbox.qualifyTableName(groupId, "data_source_item");
/*     */   }
/*     */   
/*     */   public static String getTableName(Mailbox mbox) {
/* 772 */     return DbMailbox.qualifyTableName(mbox, "data_source_item");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/DbDataSource.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */